//
//  RemarkTableViewCell.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/21.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit

class RemarkAndCouponTableViewCell: UITableViewCell {
    
    @IBOutlet weak var placeHolderLabel: UILabel!
    @IBOutlet weak var countLabel: UILabel!
    @IBOutlet weak var coupon: UILabel!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var couponViewHeight: NSLayoutConstraint!
    
    var seleCoupon:TouchBlock!
    
    var backRemarkString:BackStringValue!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.textView.delegate = self
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    // MARK:选择优惠券
    @IBAction func seleCoupon(_ sender: UIButton) {
        if self.seleCoupon != nil {
            self.seleCoupon()
        }
    }
}

extension RemarkAndCouponTableViewCell:UITextViewDelegate{
    func textViewDidChange(_ textView: UITextView) {
        let toBeString = textView.text!
        let lang = textView.textInputMode?.primaryLanguage
        if lang == "zh-Hans" {
            let selectedRange = textView.markedTextRange
            if selectedRange == nil && toBeString.count > 100{
                let endIndex = toBeString.index(toBeString.startIndex, offsetBy: 100)
                textView.text = String(toBeString[..<endIndex])
            }
        }else if toBeString.count > 100 {
            let endIndex = toBeString.index(toBeString.startIndex, offsetBy: 100)
            textView.text = String(toBeString[..<endIndex])
        }
        self.countLabel.text = "\(textView.text.count)" + "/100"
        
        self.placeHolderLabel.isHidden = textView.text.count == 0 ? false : true
        if self.backRemarkString != nil {
            self.backRemarkString(textView.text!)
        }
    }
}




















